﻿#region imports

using System;
using System.Data.Services;
using System.Data.Services.Common;
using Codeplex.ODataTFS.Model.Helpers;
using Microsoft.Data.Services.Toolkit.Providers;

#endregion

namespace Codeplex.ODataTFS.Model.Entities
{
	[HasStream]
	[DataServiceKey("Id")]
	[ETag("AttachedTime")]
	[EntityPropertyMapping("AttachedTime", SyndicationItemProperty.Updated, SyndicationTextContentKind.Plaintext, true)]
	[EntityPropertyMapping("Name", SyndicationItemProperty.Title, SyndicationTextContentKind.Plaintext, true)]
	[EntityPropertyMapping("Comment", SyndicationItemProperty.Summary, SyndicationTextContentKind.Plaintext, true)]
	public class Attachment : IStreamEntity
	{
		public string Id { get; set; }

		public int WorkItemId { get; set; }

		public int Index { get; set; }

		public DateTime AttachedTime { get; set; }

		public DateTime CreationTime { get; set; }

		public DateTime LastWriteTime { get; set; }

		public string Name { get; set; }

		public string Extension { get; set; }

		public string Comment { get; set; }

		public long Length { get; set; }

		public string Uri { get; set; }

		public string GetContentTypeForStreaming()
		{
			return RegistryHelper.GetMimeType(Extension);
		}

		public string GetStreamETag()
		{
			return "\"" + AttachedTime + "\"";
		}

		public Uri GetUrlForStreaming()
		{
			return !string.IsNullOrWhiteSpace(Uri) ? new Uri(Uri, UriKind.RelativeOrAbsolute) : new Uri("http://temp-uri", UriKind.RelativeOrAbsolute);
		}
	}
}
